
#include "not_equal_tiling.h"
#include "register/op_def_registry.h"
#include "tiling/platform/platform_ascendc.h"
#include <algorithm>

namespace optiling {
    const uint32_t BLOCK_SIZE = 64;
static ge::graphStatus TilingFunc(gert::TilingContext* context)
{

  NotEqualTilingData tiling;
      uint64_t ubSize;
    auto ascendcPlatform = platform_ascendc::PlatformAscendC(context->GetPlatformInfo());
    ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ubSize); //获取硬件平台存储空间 UB 的内存大小

    //获取输入shape信息
    uint32_t x1Size = context->GetInputShape(0)->GetStorageShape().GetShapeSize();
    uint32_t x2Size = context->GetInputShape(1)->GetStorageShape().GetShapeSize();
    uint32_t ySize = context->GetOutputShape(0)->GetStorageShape().GetShapeSize();

    uint32_t inputNum = context->GetOutputShape(0)->GetStorageShape().GetShapeSize(); //输入数量
    // if(inputNum < x1Size) inputNum = x1Size;
    // if(inputNum < x2Size) inputNum = x2Size;

    uint32_t inputBytes = GetSizeByDataType(context->GetInputDesc(0)->GetDataType()); //输入类型
    uint32_t inputLength = inputBytes * inputNum; //输入长度

    //可使用的ub空间 输入3输出1，手动考虑双缓存
    uint32_t ubDataNumber = 3*2+4;
    if(inputBytes == 1)
        ubDataNumber = 3*2+11;
    else if(inputBytes == 4)
        ubDataNumber = 3*2+6;

    // The number of 32B data blocks that can be used for each data. DOUBLE BUFFER is already counted here
    uint32_t tileBlockNum = (ubSize / BLOCK_SIZE) / ubDataNumber; //每个ub段可用的空间块数
    uint32_t tileDataNum = (tileBlockNum * BLOCK_SIZE) / inputBytes; //每次处理的数据量

    // Input data for 32B alignment
    uint32_t inputLengthAlgin32 = (((inputLength + BLOCK_SIZE - 1) / BLOCK_SIZE) * BLOCK_SIZE); //输入长度 对齐处理
    // There is at least 32B of data on each core, satisfying several settings for several cores. The maximum number of audits is the actual number of audits
    uint32_t everyCoreInputBlockNum = inputLengthAlgin32 / BLOCK_SIZE;// 输入数据需要多少空间块
    // uint32_t tailBlockNum = (inputLengthAlgin32 / BLOCK_SIZE) % aivNum;
    
    //  chunks are calculated and sliced several times using the number of data on each core
    uint32_t CoreDataNum = everyCoreInputBlockNum * BLOCK_SIZE / inputBytes; //对齐空间后的输入数量
    uint32_t TileNum = everyCoreInputBlockNum / tileBlockNum;
    uint32_t finalTileNum = (everyCoreInputBlockNum % tileBlockNum) == 0 ? TileNum : TileNum + 1; //需要循环处理几次
    // Tail block calculation for  chunks of data
    uint32_t TailDataNum = CoreDataNum - (tileDataNum * TileNum);
    TailDataNum = TailDataNum == 0 ? tileDataNum : TailDataNum; //最后一次需要处理的数据量
    TailDataNum = (((TailDataNum + BLOCK_SIZE - 1) / BLOCK_SIZE) * BLOCK_SIZE);
    
    tiling.set_CoreDataNum(CoreDataNum);  //对齐空间后的输入数量
    tiling.set_finalTileNum(finalTileNum);//需要循环处理几次
    tiling.set_tileDataNum(tileDataNum); //每次处理的数据量
    tiling.set_TailDataNum(TailDataNum); //最后一次需要处理的数据量

    // uint32_t x1Size = context->GetInputShape(0)->GetStorageShape().GetShapeSize();
    // uint32_t x2Size = context->GetInputShape(1)->GetStorageShape().GetShapeSize();
    // uint32_t ySize = context->GetOutputShape(0)->GetStorageShape().GetShapeSize();
    
    if(ySize != x1Size || ySize != x2Size)
    {
        context->SetTilingKey(2);

        int32_t y_ndarray[20], x1_ndarray[20], x2_ndarray[20];
        int32_t y_dimensional, x1_dimensional, x2_dimensional;
        auto shape_y = context->GetOutputShape(0)->GetOriginShape();
        auto shape_x1 = context->GetInputTensor(0)->GetOriginShape();
        auto shape_x2 = context->GetInputTensor(1)->GetOriginShape();

        y_dimensional =  shape_y.GetDimNum();
        x1_dimensional =  shape_x1.GetDimNum();
        x2_dimensional =  shape_x2.GetDimNum();

        int32_t max_y_dimensional;
        max_y_dimensional = y_dimensional;
        if(x1_dimensional > max_y_dimensional) max_y_dimensional = x1_dimensional;
        if(x2_dimensional > max_y_dimensional) max_y_dimensional = x2_dimensional;

        for(int i = 0; i < max_y_dimensional; i++)
        {
            // y_ndarray[y_dimensional-i-1] = shape_y.GetDim(i);
            if(i<y_dimensional) y_ndarray[y_dimensional-i-1] = shape_y.GetDim(i);
            else                    y_ndarray[i] = 1;
            if(i<x1_dimensional) x1_ndarray[x1_dimensional-i-1] = shape_x1.GetDim(i);
            else                    x1_ndarray[i] = 1;
            if(i<x2_dimensional) x2_ndarray[x2_dimensional-i-1] = shape_x2.GetDim(i);
            else                  x2_ndarray[i] = 1;
        }
        
        tiling.set_y_dimensional(max_y_dimensional);
        tiling.set_y_ndarray(y_ndarray);
        tiling.set_x1_ndarray(x1_ndarray);
        tiling.set_x2_ndarray(x2_ndarray);

        int32_t y_sumndarray[20], x1_sumndarray[20], x2_sumndarray[20];
        y_sumndarray[0] = 1;
        x1_sumndarray[0] = 1;
        x2_sumndarray[0] = 1;
        for(int i = 1; i <= max_y_dimensional; i++)
        {
            y_sumndarray[i] = y_sumndarray[i-1]*y_ndarray[i-1];
            x1_sumndarray[i] = x1_sumndarray[i-1]*x1_ndarray[i-1];
            x2_sumndarray[i] = x2_sumndarray[i-1]*x2_ndarray[i-1];
        }
        tiling.set_y_sumndarray(y_sumndarray);
        tiling.set_x1_sumndarray(x1_sumndarray);
        tiling.set_x2_sumndarray(x2_sumndarray);
    }
    else
    {
        context->SetTilingKey(1);
    }
    
    context->SetBlockDim(1);
    tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
    context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
    size_t *currentWorkspace = context->GetWorkspaceSizes(1);
    currentWorkspace[0] = 0;
    return ge::GRAPH_SUCCESS;
}
}


namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
}


namespace ops {
class NotEqual : public OpDef {
public:
    explicit NotEqual(const char* name) : OpDef(name)
    {
        this->Input("x1")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_INT32, ge::DT_INT8})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("x2")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_INT32, ge::DT_INT8})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("y")
            .ParamType(REQUIRED)
            .DataType({ge::DT_BOOL, ge::DT_BOOL, ge::DT_BOOL, ge::DT_BOOL})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});

        this->SetInferShape(ge::InferShape);

        this->AICore()
            .SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend310b");

    }
};

OP_ADD(NotEqual);
}
